<script setup lang="ts">
import { ref, onBeforeUnmount } from 'vue'
import { bus } from '@/utils/mitt'
import { changeIdentityToUserAPI } from '@/http/admin'
import { ElMessage } from 'element-plus'

const deleteAdminDialog = ref()
const userid = ref();
(bus as any).on('deleteId', (id: number) => {
  console.log(id)
  userid.value = id
})
const open = () => {
  deleteAdminDialog.value = true
}
const emit = defineEmits(['adminList'])
const onSubmitDeleteAdmin = async () => {
  console.log(userid.value)
  const res = await changeIdentityToUserAPI(userid.value)
  console.log(res)
  if (res.status == 0) {
    ElMessage({
      type: 'success',
      message: `${res.message}`,
    })
    emit('adminList')
    deleteAdminDialog.value = false
  } else {
    ElMessage.error('降级失败')
  }
}
defineExpose({
  open,
})
onBeforeUnmount(() => {
  bus.all.clear()
})
</script>

<template>
  <el-dialog v-model="deleteAdminDialog" title="删除操作" width="500" center>
    <span>是否去掉此用户的管理员职位？删除后此用户将重新展现在用户列表中</span>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="deleteAdminDialog = false">取消</el-button>
        <el-button type="primary" @click="onSubmitDeleteAdmin">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped lang="scss"></style>
